package com.hq.fxh.No3_TuiGuang.Fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.GetPushBaseInfoRes;
import com.hq.fxh.Modle.GetPushStatisticListRes;
import com.hq.fxh.Modle.GetPushStatisticListResMonth;
import com.hq.fxh.No0_ChengQu.Fragment.BaseFragment;
import com.hq.fxh.No3_TuiGuang.Activity.PushDetailInfoActivity;
import com.hq.fxh.No3_TuiGuang.Activity.PushStatisticDataActivity;
import com.hq.fxh.No3_TuiGuang.Adaoter.PushStatisticGroupAdapter;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.R;
import com.hq.fxh.Utils.IntentUtils;
import com.hq.fxh.Utils.LongNumTransUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;

/*
* 企业推广 --互推页面
* */
public class Tg_HuTui_Fragment extends BaseFragment {

    private RecyclerView recyclerView;
    private List<GetPushStatisticListResMonth> pushListData = new ArrayList<>();
    private PushStatisticGroupAdapter adapter;
    private LinearLayout ll_emptyDate;
    private LinearLayout ll_pushInfo;
    private TextView company_name;
    private TextView explain;
    private TextView adExportCount;
    private TextView adClickCount;
    private TextView adClickRate;
    private LinearLayout ll_adExport;
    private LinearLayout ll_adClick;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_inter_promotion;
    }

    @Override
    protected void findView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        ll_emptyDate = (LinearLayout) view.findViewById(R.id.ll_emptyDate);
        ll_pushInfo = (LinearLayout) view.findViewById(R.id.ll_pushInfo);
        ll_adExport = (LinearLayout) view.findViewById(R.id.ll_adExport);
        ll_adClick = (LinearLayout) view.findViewById(R.id.ll_adClick);
        company_name = (TextView) view.findViewById(R.id.company_name);
        explain = (TextView) view.findViewById(R.id.explain);
        adExportCount = (TextView) view.findViewById(R.id.adExportCount);
        adClickCount = (TextView) view.findViewById(R.id.adClickCount);
        adClickRate = (TextView) view.findViewById(R.id.adClickRate);
    }

    @Override
    protected void initData() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(myActivity);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        getPushBaseInfo();
        getPushStatisticList();
        registerListener();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            JAnalyticsInterface.onPageStart(getContext(), "推广-互推");
        } else {
            JAnalyticsInterface.onPageEnd(getContext(), "推广-互推");
        }
    }

    private void getPushBaseInfo() {
        UserServiceImpl.getPushBaseInfo(new NetworkSubscriber<GetPushBaseInfoRes>() {
            @Override
            public int onSuccess(GetPushBaseInfoRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    GetPushBaseInfoRes.DataBean pushBaseInfo = data.getData();
                    if (!TextUtils.isEmpty(pushBaseInfo.getCompany_name())) {
                        company_name.setText(pushBaseInfo.getCompany_name());
                        company_name.setCompoundDrawables(null, null, null, null);
                    } else {
                        company_name.setText("互推模板：暂无互推的广告模板");
                 /*       company_name.setCompoundDrawables(null, myActivity.getResources().getDrawable(R.drawable.ht_shuom), null, null);
                        company_name.setCompoundDrawablePadding(6);*/
                    }
                    explain.setText("互推模板："+pushBaseInfo.getAdvert_template_name());
                    DecimalFormat df = new DecimalFormat("###0.00");
                    adClickRate.setText(df.format(pushBaseInfo.getProbability()) + "");
                    final Object[] exportNum = LongNumTransUtils.calculateNum(pushBaseInfo.getNum_look());
                    adExportCount.setText(exportNum[0] + "" + exportNum[1]);
                    final Object[] clickNum = LongNumTransUtils.calculateNum(pushBaseInfo.getNum_click());
                    adClickCount.setText(clickNum[0] + "" + clickNum[1]);
                } else {
                    ToastUtils.show(getContext(), data.getMsg());
                }
                return 0;
            }
        });
    }

    private void registerListener() {
        ll_pushInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtils.openActivity(getContext(), PushDetailInfoActivity.class);
            }
        });
        ll_adExport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("position", 0);
                IntentUtils.openActivity(myActivity, PushStatisticDataActivity.class, bundle);
            }
        });
        ll_adClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("position", 1);
                IntentUtils.openActivity(myActivity, PushStatisticDataActivity.class, bundle);
            }
        });
    }

    private void getPushStatisticList() {
        UserServiceImpl.getPushStatisticList(new NetworkSubscriber<GetPushStatisticListRes>() {
            @Override
            public int onSuccess(GetPushStatisticListRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0 && data.getData().size() > 0) {
                    recyclerView.setVisibility(View.VISIBLE);
                    ll_emptyDate.setVisibility(View.GONE);
                    pushListData.clear();
                    pushListData.addAll(data.getData());
                    if (adapter == null) {
                        adapter = new PushStatisticGroupAdapter(myActivity, pushListData);
                        recyclerView.setAdapter(adapter);
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    recyclerView.setVisibility(View.GONE);
                    ll_emptyDate.setVisibility(View.VISIBLE);
                }
                return 0;
            }
        });
    }
}

